<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Node(val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
        var a = new Node(1)
        var b = new Node(2)
        var c = new Node(2)
        var d = new Node(3)
        var e = new Node(4)
        var f = new Node(4)
        var g = new Node(3)
        a.left = b
        a.right = c
        b.left = d
        b.right = e
        c.left = f
        c.right = g

        /* 
            时间: O(n)
            空间: O(n) 最坏情况，二叉树退化成一条链?我咋觉得应该是O(1)
        */
        var isSymmetric = function (root) {
            if (!root) return root
            let queue = []
            queue.push(root.left)
            queue.push(root.right)
            while (queue.length) {
                // 如果是栈，就是这里先后顺序反一下

                // 拿出左右孩子比较
                let leftNode = queue.shift()
                let rightNode = queue.shift()

                if (!leftNode && !rightNode) {
                    continue
                }

                if (!leftNode || !rightNode || leftNode.val !== rightNode.val) {
                    return false
                }

                // 左节点左孩子
                queue.push(leftNode.left)
                // 右节点右孩子
                queue.push(rightNode.right)
                queue.push(leftNode.right)
                queue.push(rightNode.left)
            }
            return true
        }
        console.log(isSymmetric(a));
    </script>
</body>

</html>